﻿using Microsoft.AspNet.SignalR.Client;
using Model;
using Model.Modules;
using Newtonsoft.Json;
using Proxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ViewModel
{
    public class VM_WindowHistoryAlert : BaseViewModel
    {
        public static surfacedetectEntities surfaceEntities;
        public ObservableCollection<alert_info> Alert_infoList { get; set; }
        public DateTime Start { get; set; }
        public DateTime End { get; set; }


        public VM_WindowHistoryAlert()
        {
            initData();
        }
        void initData()
        {
            Start = DateTime.Now.AddDays(-1);
            End = DateTime.Now;
            surfaceEntities = new surfacedetectEntities();
            Alert_infoList = new ObservableCollection<alert_info>(surfaceEntities.alert_info.Where(t => t.insert_time >= Start && t.insert_time <= End).OrderByDescending(o => o.insert_time).Take(1000));
        }

        public BaseCommand SearchAlertCommand
        {
            get => new BaseCommand((o) =>
            {
                Alert_infoList = new ObservableCollection<alert_info>(surfaceEntities.alert_info.Where(a => a.insert_time >= Start && a.insert_time <= End).OrderByDescending(a => a.insert_time).Take(1000));
            });
        }

        public BaseCommand SaveCommand
        {
            get => new BaseCommand((o) =>
            {
                surfaceEntities.SaveChanges();
            });
        }
        public BaseCommand ConfirmAlertInfo
        {
            get => new BaseCommand((obj) =>
            {
                var currentinfo = Alert_infoList.Where(t => t.id == obj.ToString());
                if (currentinfo.Count() == 1)
                {
                    currentinfo.First().confirmed = true;
                    surfaceEntities.Entry(currentinfo.First()).State = EntityState.Modified;
                }
            });
        }
    }
}
